package per.wxp.compute_service.web;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ComputeController {

	private final Logger logger = Logger.getLogger(getClass());
	
    @Autowired
    private DiscoveryClient client;

    @RequestMapping(value = "/add" ,method = RequestMethod.GET)
    public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
        ServiceInstance instance = client.getLocalServiceInstance();
        Integer r = a + b;
        logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
        return r;
    }
    @RequestMapping(value = "/map", method = RequestMethod.GET)
	public Map<String, Object> index() {
		Map<String, Object> map = new HashMap<>();
		map.put("key", 1);
		map.put("ll", "value");

		ServiceInstance instance = client.getLocalServiceInstance();
		logger.info("/add, host:" + instance.getHost() + ", service_id:"
				+ instance.getServiceId() + ", result:");
		return map;
	}

	@RequestMapping(value = "/string", method = RequestMethod.GET)
	public String string() throws InterruptedException {

		String a = "hhhhh";
		
		ServiceInstance instance = client.getLocalServiceInstance();
		
		int sleepTime=new Random().nextInt();
		logger.info(sleepTime);
		Thread.sleep(sleepTime);
		logger.info("/add, host:" + instance.getHost() + ", service_id:"
				+ instance.getServiceId() + ", result:");
		return a;
	}
}
